<script setup lang="ts">
const dialog = reactive({
  visible: false,
  title: "分配客户",
  width: "300",
});

const allocateId = ref(0);
const memberId = ref(0);

import { shareCustomer } from "@/api/customer/allocate";
const ruleFormRef = ref(ElForm);
const ruleForm = reactive({
  user_id: 0,
});
const submitShare = () => {
  if (ruleForm.user_id === 0) {
    ElMessage.warning("请选择员工");
    return;
  }

  ruleFormRef.value.validate(async () => {
    const postData = {
      user_id: ruleForm.user_id,
    };
    const { data } = await shareCustomer(
      memberId.value,
      allocateId.value,
      postData
    );
    if (data) {
      closeDialog();
      ElMessage.success("分配成功");
    }
  });
};

// 获取所有员工
import { UserItem } from "@/api/user/types";
import { getUserList } from "@/api/user";
const staffOptions = ref<UserItem[]>([]);
// 获取所有员工
const initStaffList = async () => {
  const { data } = await getUserList();
  data.unshift({ id: 0, realname: "请选择", nickname: "请选择" });
  staffOptions.value = data;
};

const showDialog = (allocate_id: number, member_id: number) => {
  allocateId.value = allocate_id;
  memberId.value = member_id;
  dialog.visible = true;
  initStaffList();
};

const closeDialog = () => {
  dialog.visible = false;
  allocateId.value = 0;
  memberId.value = 0;
};

defineExpose({
  showDialog,
});
</script>
<template>
  <el-dialog
    class="form-dialog"
    v-model="dialog.visible"
    :title="dialog.title"
    :width="dialog.width"
    append-to-body
    @close="closeDialog"
    center
  >
    <el-form ref="ruleFormRef" :model="ruleForm">
      <el-form-item label="请选择">
        <el-select v-model="ruleForm.user_id" placeholder="请选择">
          <el-option
            v-for="item in staffOptions"
            :key="item.id"
            :label="item.realname"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submitShare">确 定</el-button>
    </span>
  </el-dialog>
</template>
